package com.nanhu.test;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.ShapeDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class CanvasTestActivity extends Activity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ScrollView scrollView = new ScrollView(this);
		ViewGroup.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
		scrollView.setLayoutParams(params);
		
		LinearLayout linearLayout = new LinearLayout(this);
		scrollView.addView(linearLayout, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
		linearLayout.setOrientation(LinearLayout.VERTICAL);
		
		DrawView drawView = new DrawView(this);
		drawView.setMinimumHeight(500);
		drawView.setMinimumWidth(300);
		linearLayout.addView(drawView);
		
		TestTwoView viewTwo = new TestTwoView(this);
		//viewTwo.setMinimumHeight(300);
		//viewTwo.setMinimumWidth(300);
		//linearLayout.addView(viewTwo);
		TextView tv;
		linearLayout.addView(viewTwo, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		
		setContentView(scrollView);
	}
	
	public class TestTwoView extends View{

		public TestTwoView(Context context) {
			super(context);
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			
			Paint p = new Paint();
			//边框
			p.setColor(Color.WHITE);
			p.setStyle(Paint.Style.STROKE);
			canvas.drawRect(1, 1, 300, 200, p);
		}
	}
	
	public class DrawView extends View{

		public DrawView(Context context) {
			super(context);
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			
			Paint p = new Paint();
			//边框
			p.setColor(Color.WHITE);
			p.setStyle(Paint.Style.STROKE);
			canvas.drawRect(1, 1, 300, 420, p);
			//圆
			p.setColor(Color.RED);
			canvas.drawText("画圆：", 10, 20, p);
			canvas.drawCircle(60, 20, 10, p);
			p.setAntiAlias(true);// 设置画笔的锯齿效果。 true是去除
			canvas.drawCircle(120, 20, 20, p);// 大圆
			//线、弧线
			canvas.drawText("弧线：", 10, 60, p);
			p.setColor(Color.GREEN);
			canvas.drawLine(60, 40, 100, 40, p);
			canvas.drawLine(110, 40, 190, 80, p);
			RectF oval1 = new RectF(150, 20, 180, 40);
			//canvas.drawRect(oval1, p);
			canvas.drawArc(oval1, 180, 180, false, p);
			oval1.set(190, 20, 220, 40);
			//canvas.drawRect(oval1, p);
			canvas.drawArc(oval1, 180, 180, false, p);
			oval1.set(160, 30, 210, 60);
			//canvas.drawRect(oval1, p);
			canvas.drawArc(oval1, 0, 180, false, p);
			//矩形
			canvas.drawText("矩形", 10, 80, p);
			p.setColor(Color.GRAY);
			p.setStyle(Paint.Style.FILL);
			canvas.drawRect(60, 60, 80, 80, p);
			canvas.drawRect(60, 90, 160, 100, p);
			//扇形、椭圆
			p.setStyle(Paint.Style.STROKE);
			canvas.drawText("扇形、椭圆", 10, 120, p);
			Shader mShader = new LinearGradient(0, 0, 100, 100, new int[]{Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW, Color.LTGRAY}, null, TileMode.REPEAT);
			p.setShader(mShader);
			RectF oval2 = new RectF(60, 100, 200, 240);
			canvas.drawRect(oval2, p);
			canvas.drawArc(oval2, 200, 130, true, p);
			
			oval2.set(210, 100, 250, 130);
			canvas.drawRect(oval2, p);
			canvas.drawOval(oval2, p);
			//三角形
			canvas.drawText("三角形：", 10, 200, p);
			Path path = new Path();
			path.moveTo(80, 200);
			path.lineTo(120, 250);
			path.lineTo(80, 250);
			path.close();
			canvas.drawPath(path, p);
			//圆角矩形
			p.setStyle(Paint.Style.FILL);
			p.setColor(Color.LTGRAY);
			p.setAntiAlias(true);
			canvas.drawText("圆角矩形", 10, 260, p);
			RectF oval3 = new RectF(80, 260, 200, 300);
			canvas.drawRoundRect(oval3, 20, 15, p);
			p.setStyle(Paint.Style.STROKE);
			canvas.drawRect(oval3, p);
			//贝塞尔曲线
			canvas.drawText("贝塞尔曲线", 10, 310, p);
			p.reset();
			p.setStyle(Paint.Style.STROKE);
			p.setColor(Color.GREEN);
			Path path2 = new Path();
			path2.moveTo(100, 320);
			path2.quadTo(150, 310, 170, 400);
			canvas.drawPath(path2, p);
			//点
			p.setStyle(Paint.Style.FILL);
			canvas.drawText("点：", 10, 390, p);
			canvas.drawPoint(60, 390, p);
			canvas.drawPoints(new float[]{60, 400, 65, 400, 70, 400}, p);
			//图片
			Bitmap bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
			canvas.drawBitmap(bitMap, 250, 360, p);
			
			//填充多边形
			Path path3 = new Path();
			path3.moveTo(50, 50);
			path3.lineTo(100, 50);
			path3.lineTo(100, 0);
			path3.lineTo(150, 0);
			path3.lineTo(150, 50);
			path3.lineTo(200, 50);
			path3.lineTo(200, 100);
			path3.lineTo(250, 100);
			path3.lineTo(250, 150);
			path3.lineTo(200, 150);
			path3.lineTo(200, 200);
			path3.lineTo(150, 200);
			path3.lineTo(150, 250);
			path3.lineTo(100, 250);
			path3.lineTo(100, 200);
			path3.lineTo(50, 200);
			path3.lineTo(50, 150);
			path3.lineTo(0, 150);
			path3.lineTo(0, 100);
			path3.lineTo(50, 100);
			path3.close();
			//生成多边形
			ShapeDrawable mDrawable = new ShapeDrawable();
			Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
			Bitmap b1 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight());
			Shader shader = new BitmapShader(b1, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
			mDrawable.getPaint().setShader(shader);
			mDrawable.setBounds(10, 390, 50, 390);
		}
	}
}
